package me.mouse.gokistats.listener;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import me.mouse.gokistats.GokiStats;
import me.mouse.gokistats.config.Config;
import me.mouse.gokistats.config.Language;
import me.mouse.gokistats.event.MenuClickEvent;
import me.mouse.gokistats.event.MenuLoadTalentItemEvent;
import me.mouse.gokistats.talent.RegisterTalent;
import me.mouse.gokistats.talent.Talent;
import me.mouse.gokistats.util.PlayerData;

public class GokiStatsListener implements Listener {
	@EventHandler
	public void onInventoryClick(InventoryClickEvent e) {
		String t = e.getInventory().getTitle();
		if (t.startsWith("GokiStats")) {
			Player player = (Player) e.getWhoClicked();
			int slot = e.getRawSlot();
			int page = Integer.parseInt(t.substring(10));
			ClickType type = e.getClick();
			MenuClickEvent event = new MenuClickEvent(player, slot, type, page);
			Bukkit.getServer().getPluginManager().callEvent(event);
			e.setCancelled(true);
			return;
		}
	}

	@EventHandler
	public void onPlayerJoin(PlayerJoinEvent e) {
		GokiStats.loadEffect(e.getPlayer());
	}

	@EventHandler
	public void onPlayerRespawn(PlayerRespawnEvent e) {
		GokiStats.loadEffect(e.getPlayer());
	}

	@EventHandler
	public void onPlayerQuit(PlayerQuitEvent e) {
		e.getPlayer().setMaxHealth(20.0D);
		e.getPlayer().setWalkSpeed(0.2F);
	}

	@EventHandler
	public void onPlayerDeath(PlayerDeathEvent e) {
		if (Config.getDeathClearTalent()) {
			Player p = e.getEntity();
			PlayerData data = new PlayerData(p);
			data.clearTalent();
			data.save();
		}
	}

	@EventHandler
	public void onMenuClick(MenuClickEvent e) {
		int page = e.getPage();
		int slot = e.getSlot() + (page - 1) * 54;
		Player player = e.getPlayer();
		List<Talent> Talents = RegisterTalent.getRegisterTalent();
		for (Talent t : Talents) {
			if (t.getSlot() == slot) {
				int level = GokiStats.getLevel(player, t);
				if (level >= t.getMaxLevel()) {
					player.sendMessage(Language.perfix + Language.levelMax);
					if (!Config.getKeepMenu()) {
						e.getPlayer().closeInventory();
					}
					return;
				}
				boolean b = GokiStats.addLevel(e.getPlayer(), t);
				if (b) {
					player.sendMessage(Language.perfix + Language.success);
					e.getPlayer().closeInventory();
					if (Config.getKeepMenu())
						GokiStats.openMenu(player, page);
				} else {
					player.sendMessage(Language.perfix + Language.failure);
					if (!Config.getKeepMenu()) {
						e.getPlayer().closeInventory();
					}
				}
				return;
			}
		}
		player.sendMessage(Language.perfix + Language.unknownTalent);
		if (!Config.getKeepMenu())
			e.getPlayer().closeInventory();
	}

	@EventHandler
	public void onMenuLoadTalentItem(MenuLoadTalentItemEvent e) {
		String srep;
		double rep;
		DecimalFormat dcmFmt = new DecimalFormat("###.##");
		switch (e.getTalent().getName()) {
		case "heal":
			rep = GokiStats.getLevel(e.getPlayer(), GokiStats.getHeal())
					* GokiStats.config.getDouble("talent.heal.heal", 4.0D) / 2.0D;
			break;
		case "speed":
			rep = GokiStats.getLevel(e.getPlayer(), GokiStats.getSpeed())
					* GokiStats.config.getDouble("talent.speed.speed", 5.0D);
			break;
		case "sword":
			rep = GokiStats.getLevel(e.getPlayer(), GokiStats.getSword())
					* GokiStats.config.getDouble("talent.sword.sword", 2.0D);
			break;
		case "armor":
			rep = GokiStats.getLevel(e.getPlayer(), GokiStats.getArmor())
					* GokiStats.config.getDouble("talent.armor.armor", 2.0D);
			break;
		case "bow":
			rep = GokiStats.getLevel(e.getPlayer(), GokiStats.getBow())
					* GokiStats.config.getDouble("talent.bow.bow", 2.0D);
			break;
		case "axe":
			rep = GokiStats.getLevel(e.getPlayer(), GokiStats.getAxe())
					* GokiStats.config.getDouble("talent.axe.axe", 2.0D);
			break;
		case "deathgod":
			rep = GokiStats.getLevel(e.getPlayer(), GokiStats.getDeathGod())
					* GokiStats.config.getDouble("talent.deathgod.deathgod", 0.01D);
			break;
		case "rubber":
			rep = GokiStats.getLevel(e.getPlayer(), GokiStats.getRubber())
					* GokiStats.config.getDouble("talent.rubber.rubber", 2.0D);
			break;
		case "fire":
			rep = GokiStats.getLevel(e.getPlayer(), GokiStats.getFire())
					* GokiStats.config.getDouble("talent.fire.fire", 2.0D);
			break;
		case "fall":
			rep = GokiStats.getLevel(e.getPlayer(), GokiStats.getFall())
					* GokiStats.config.getDouble("talent.fall.fall", 2.0D);
			break;
		default:
			rep = 0D;
			break;
		}
		srep = dcmFmt.format(rep);
		ItemStack item = e.getItem();
		ItemMeta meta = item.getItemMeta();
		List<String> Lore = meta.getLore();
		List<String> NewLore = new ArrayList<String>();
		for (String s : Lore) {
			s = s.replaceAll("%" + e.getTalent().getName(), srep);
			NewLore.add(s);
		}
		meta.setLore(NewLore);
		item.setItemMeta(meta);
		e.setItem(item);
	}
}
